package com.hd.trans.files.holder;

import android.util.SparseArray;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.DrawableRes;
import androidx.annotation.IdRes;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

public class BaseViewHolder extends RecyclerView.ViewHolder {

    private final SparseArray<View> mViews = new SparseArray<>();

    public BaseViewHolder(@NonNull View itemView) {
        super(itemView);
    }

    public void setText(@IdRes int id, String text) {
        TextView textView = getView(id);
        textView.setText(text);
    }

    public void setImageRes(@IdRes int id, @DrawableRes int drawableId) {
        ImageView imageView = getView(id);
        imageView.setImageResource(drawableId);
    }

    protected void setViewGone(@IdRes int id, boolean gone) {
        View view = itemView.findViewById(id);
        if (gone) {
            view.setVisibility(View.GONE);
        } else {
            view.setVisibility(View.VISIBLE);
        }
    }

    public <V extends View> V getView(@IdRes int viewId) {
        View view = mViews.get(viewId);
        if (view == null) {
            view = itemView.findViewById(viewId);
            mViews.put(viewId, view);
        }
        return (V) view;
    }
}
